package sql.show.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddProductActivity extends Activity implements OnClickListener{

	private Button btn_addrecord;
    private EditText txtpname, txtpprice, txtpid;
    DataBaseHelper db;
    ProductList product;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addproduct);

        txtpname = (EditText) findViewById(R.id.txtpname);
        txtpprice = (EditText) findViewById(R.id.txtpprice);
        btn_addrecord = (Button) findViewById(R.id.btn_addrecord);

        txtpid = (EditText) findViewById(R.id.txtpid);
        btn_addrecord.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_addrecord:

            if (txtpid.getText().toString().equals("")
            	||txtpname.getText().toString().equals("")
                || txtpprice.getText().toString().equals("")) {
                Toast.makeText(AddProductActivity.this, "Please add values..",
                        Toast.LENGTH_LONG).show();
            } else {
            	/**
            	 * đối tượng db chỉ sử dụng 1 lần, sao không để biến trong hàm (local ) mà để ngoài hàm ( field ) vậy Trung ?
            	 * sao không dùng cách này  
            	 * 
            	 * DataBaseHelper db = new DataBaseHelper(getApplicationContext());
            	 * ProductList product = new ProductList();
            	 * @author Kypi221
            	 */
                db = new DataBaseHelper(getApplicationContext());
                db.getWritableDatabase();
                product = new ProductList();
                product.idProduct = (txtpid.getText().toString());
                product.nameProduct = txtpname.getText().toString();
                product.priceProduct = txtpprice.getText().toString();

                
                   db.addProduct(product);
                Toast.makeText(AddProductActivity.this, "Record Added successfully.",
                        Toast.LENGTH_LONG).show();
                finish();
            }
            break;

        default:
            break;
        }

    }
}
